#include "MCU_include.h"

vu8 OVP_Delay_Cnt =0;
vu8 CheckPanTiming = 0;
vu8 CheckPhaseNum=0;
vu8	Current_Offset=0;
volatile bit OVP2_Over_I_flag;		
volatile bit OVP4_Over_I_flag;		
volatile bit OVP3_Over_I_flag;
volatile bit OVP5_Over_I_flag;
/********************************************************************
Function: Interrupt
INPUT   :
OUTPUT  :
NOTE    :
********************************************************************/
void Interrupt_Init()
{

	CTM0_ISR_ON();
	CTM1_ISR_ON();
	CTM2_ISR_ON();
	_ovp1f  = 0;
	_ovp1e  = 0;	
	_mf5e   = 1;
	_mf1e   = 1;
	_mf1f   = 0;
	_ovp2e  = 1;	
	_ovp4e  = 1;		
	_ovp6f	= 0;
	_ovp6e	= 1;				
	_ovp7e  = 1;	
	_ovp7f  = 0;
	_mf0e	= 1;
	_mf0f   = 0;
	_ovp5e  = 1;
	_ovp8e 	= 1;			
	_ersge  = 1;		
//	_ersgc7 = 1;		
	_ovp0f  = 0;
	_ovp0e  = 1;
//********************************//	
	_mf2e   = 1;		//multi-function interrupt 2 control,FJTM,FJEQU
	_mf2f   = 0;
	_fjtme  = 1;		//FJTMR interrupt control
	_fjtmf  = 0;
	_fjeque = 1;
	_fjequf = 0;
//*******************************//
	_phasee = 0;
	_ovp2f  = 0;		
	_ovp4f	= 0;
	_ovp7f  = 0;		
	_ovp8f  = 0;
	_mf0f   = 0;
	_phasef = 0;		
	_ersgf  = 0;		
	_usime  = 1;	
	_emi    = 1;		
//-------------------	

	
}


/***********************************************
Function:CTM1 100us
INPUT:
OUTPUT:
************************************************/
void __attribute((interrupt(0x38)))	CTM1_A_ISR()
{
//	GCC_NOP();
	_ctm1af = 0;
	if(++OVP_Delay_Cnt > 59)	//	59*100us = 5.9ms
	{
		OVP_Delay_Cnt = 0;
		CheckPanTiming = 1;
		CTM1_OFF();		
			CheckPhaseNum=0;	
	}
}

/********************************************************************
Function: 
INPUT   :
OUTPUT  :
NOTE    :	1ms
********************************************************************/
void __attribute((interrupt(0x3C))) CTM2_A_ISR()        
{
	if (Ad_Ch_Sel == get_voltage_ADvalue)
	{
		V_Ad_Value = AD_Convert(ADC_CH9);			
		GCC_DELAY(10);
		
	}
	else if (Ad_Ch_Sel == get_current_ADvalue)
	{			  
		{
			I_Ad_Value[0] = AD_Convert(ADC_CH_INTERNAL_OPARO);	
			GCC_NOP();
		}
		
	}
	else if (Ad_Ch_Sel == get_IGBTtemp_ADvalue)
	{
		V_Ad_Value = AD_Convert(ADC_CH3);			
	}
	else if (Ad_Ch_Sel == get_TOPtemp_ADvalue)
	{
		V_Ad_Value = AD_Convert(ADC_CH5);			
	}

	Ad_Samp_Flag = 1;
		_ctm2af = 0;
	GCC_NOP();
}

/********************************************************************
Function: Boiler pulse counting
INPUT   : OVP1P 
OUTPUT  :
NOTE    :
********************************************************************/
void __attribute((interrupt(0x30))) OVP01_ISR()  //	
{
		GCC_NOP();	
}

/********************************************************************
Function:Number resonance current overcurrent protection interruption
INPUT   : OVP2P 
OUTPUT  :
NOTE    :
********************************************************************/
void __attribute((interrupt(0x18))) OVP2345_ISR()  		
{

	OVP2_Cnt = 0;
	if( _ovp4f )	
	{		
		OVP4_Over_I_flag =1;
	}				
	if( _ovp2f)
	{
		
		OVP2_Over_I_flag =1;
	}
	if( _ovp3f)
	{
		IH_Status_APP.flagBit.error = ERROR_OCP;	
		OVP3_Over_I_flag =1;
	}
	if( _ovp5f)
	{
		IH_Status_APP.flagBit.error = ERROR_OCP;
		OVP5_Over_I_flag =1;
	}
	
	errorCODE_u16 = errorCODE_u16 |0b00001000 ;		
	_ovp2f = 0;
	_ovp4f = 0;
	_ovp3f = 0;
	_ovp5f = 0;
	_mf1f = 0;			
}

/********************************************************************
Function:
INPUT   : Phase 
OUTPUT  :
NOTE    :
********************************************************************/
void __attribute((interrupt(0x0c))) Phase_ISR()
{
	_phasef = 0;

			PhaseDebounceCnt = 0;
		if (_npfg)					
		{
			_npfg = 0;
			if (CheckPhaseNum++>2)
			{
				CheckPhaseNum=0;
				IGBT_OFF1(PWM_F);
				PWM_F = d_PWM_F;
				IH_Status_APP.flagBit.error = ERROR_PHASE;	
				FG_IH_Status.flagBit.PWM_ON_OFF = 0;
				ClearMSS0_1_for_ISR();
				errorCODE_u16=errorCODE_u16 |0b00000100;
				POWER_CTRL.flagBit.FrePhase =1;	
			}
		}
		if(_spfg)						
		{				
			_spfg = 0;				
			errorCODE_u16=errorCODE_u16|0b00000001;
			_nop();	
		}	
	
}
/***********************************************
Function:Zero point detection of power，Surge protection interruption
INPUT:	OVP8&OVP6
OUTPUT:
************************************************/
void __attribute((interrupt(0x14))) OVP8_ISR()			//ovp8 &ovp6
{

	if (_ovp6f)
	{	
		GCC_NOP();
		FG_IH_Status.flagBit.PWM_ON_OFF = 0;	
		ErrRecoveryTimer = C_ErrRecoveryTime;		
		bSurge_error = 1;
		errorCODE_u16 = errorCODE_u16 | 0b00010000;		
	}	
	if(_ovp7f)
	{		
		CTM1_ON();  	
		_fjtmf=0;
		if(HF_Pwm_Freq_update_flag)
		{	
			HF_Pwm_Freq_update_flag=0;					
			HF_Pwm_Freq_update();
		}
	}
	_ovp6f = 0;
	_ovp7f = 0;
	_ovp8f = 0;
}


/********************ERSG protection interruption *********************/
void __attribute((interrupt(0x08))) ERSG_ISR()
{
	_ersgf =0;
	
	if (_phasef == 1)
	{
		_phasef = 0;
//		IH_Status_APP.flagBit.error = ERROR_PHASE;	//
//		PWM_OFF();	
//		FG_IH_Status.flagBit.PWM_ON_OFF = 0;
//		ClearMSS0_1_for_ISR();
//			bit_BUZZER = ON;
//			BuzDelay = 30;	
//			BUZZER_ON_100MS();
//		_nop();			
		errorCODE_u16=errorCODE_u16|0b00100000;	
	}
}


/********************************************************************
Function:
INPUT   :
OUTPUT  :
NOTE    :
********************************************************************/
void __attribute((interrupt(0x20))) ISR_MFI2() 
{

	fjtmer_state = _fjtmc;
	fjtmer_state = fjtmer_state & 0x70;

if(_fjtmf)	
{		
	if(fjtmer_state==0x00)		//	（t0~t1）
	{
		_nop();	
		
	}
	if(fjtmer_state==0x10)		//	（t1~t2）
	{
		_nop();	
	}
	
	if(fjtmer_state==0x20)		//	（t2~t3）
	{
		_nop();	
	}
       
	
	if(fjtmer_state==0x30)		//	（t3~t4）
	{
		_nop();	
	}
	
	if(fjtmer_state==0x40)		//	（t4~t5）
	{

		HF_Pwm_Freq_update_flag=1;		
	}	
}

	_fjtmf=0;
	_fjequf=0;
	_mf2f=0;
}


/******************* End of  file ***************/